package com.tsd.jxc.utils;

import com.tsd.core.utils.HlpUtils;
import com.tsd.jxc.goods.entity.JxcCategoryExt;
import com.tsd.jxc.stock.entity.JxcStockWarehouseExt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeUtil {
    /**
     * 构造商品类目树
     *
     * @param list
     * @return
     */
    public static List<JxcCategoryExt> expandCategorysTree(List<JxcCategoryExt> list) {
        if (HlpUtils.isEmptyList(list)) {
            return list;
        }
        List<JxcCategoryExt> resultList = new ArrayList<>();
        Map<String, JxcCategoryExt> regionMap = new HashMap<>();
        for (JxcCategoryExt assetsRegion : list) {
            regionMap.put(assetsRegion.getSid(), assetsRegion);
        }
        for (JxcCategoryExt jxcCategoryExt : list) {
            JxcCategoryExt hashVP = regionMap.get(jxcCategoryExt.getParent_sid());
            if (hashVP != null) {
                List<JxcCategoryExt> childrens = hashVP.getChildren();
                if (HlpUtils.isEmptyList(childrens)) {
                    childrens = new ArrayList<>();
                }
                childrens.add(jxcCategoryExt);
                hashVP.setChildren(childrens);
            } else {
                resultList.add(jxcCategoryExt);
            }
        }
        return resultList;
    }

    /**
     * 构造仓库树
     *
     * @param list
     * @return
     */
    public static List<JxcStockWarehouseExt> expandStockWarehouseTree(List<JxcStockWarehouseExt> list) {
        if (HlpUtils.isEmptyList(list)) {
            return list;
        }
        List<JxcStockWarehouseExt> resultList = new ArrayList<>();
        Map<String, JxcStockWarehouseExt> regionMap = new HashMap<>();
        for (JxcStockWarehouseExt warehouseExt : list) {
            regionMap.put(warehouseExt.getSid(), warehouseExt);
        }
        for (JxcStockWarehouseExt warehouseExt : list) {
            JxcStockWarehouseExt hashVP = regionMap.get(warehouseExt.getParent_sid());
            if (hashVP != null) {
                List<JxcStockWarehouseExt> childrens = hashVP.getChildren();
                if (HlpUtils.isEmptyList(childrens)) {
                    childrens = new ArrayList<>();
                }
                childrens.add(warehouseExt);
                hashVP.setChildren(childrens);
            } else {
                resultList.add(warehouseExt);
            }
        }
        return resultList;
    }

}
